package com.vikutorika.dao.imp;

import com.vikutorika.dao.BasicDAO;
import com.vikutorika.dao.ManagerAccountInfoDAO;
import com.vikutorika.entity.ManagerAccountInfo;
import com.vikutorika.util.V_DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManagerAccountInfoDAOImp extends BasicDAO<ManagerAccountInfo> implements ManagerAccountInfoDAO {
    @Override
    public List<ManagerAccountInfo> getAll() {
        return null;
    }

    @Override
    public int totalRow() {
        return 0;
    }

    @Override
    public List<ManagerAccountInfo> getByAccount(String m_account) {
        return null;
    }

    @Override
    public ManagerAccountInfo query(String m_account) throws SQLException{
        Connection connection = V_DBUtils.getConnection();
        PreparedStatement statement =
                connection.prepareStatement("select * from manager_account_info where m_account = ?");
        statement.setString(1, m_account);
        List<ManagerAccountInfo> managers = trans(statement.executeQuery());
        return managers.isEmpty() ? null:managers.get(0);
    }

    private List<ManagerAccountInfo> trans(ResultSet resultSet) throws SQLException{
        List<ManagerAccountInfo> mangers = new ArrayList<>();
        while(resultSet.next()) {
            mangers.add(new ManagerAccountInfo(resultSet.getInt("mid"),
                    resultSet.getString("m_account"),  resultSet.getString("m_password_hash")));
        }
        return mangers;
    }
}
